package com.aboo.web.common.util;

import java.util.HashMap;
import java.util.Map;

import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class AspectjBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
	private static final Map<String,Class<?>> map = new HashMap<String,Class<?>>();

	static {
		map.put(InfrastructureAdvisorAutoProxyCreator.class.getName(), InfrastructureAdvisorAutoProxyCreatorExt.class);
		map.put(AspectJAwareAdvisorAutoProxyCreator.class.getName(), AspectJAwareAdvisorAutoProxyCreatorExt.class);
		map.put(AnnotationAwareAspectJAutoProxyCreator.class.getName(), AnnotationAwareAspectJAutoProxyCreatorExt.class);
	}
	
	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException {
		BeanDefinition  bean = beanFactory.getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
		if(bean!=null){
			Class<?> clazz = map.get(bean.getBeanClassName());
			if(clazz!=null){
				bean.setBeanClassName(clazz.getName());
			}
		}
	}
}
